from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
from .models import *
from .ser import *


class Car(APIView):
    def post(self, request):
        ser = CarSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 10010})

    def get(self, request):
        id = request.GET.get('id')
        if not id:
            cars = CarTable.objects.all()
            ser = CarSer(cars, many=True)
            return Response({'code': 200, 'list': ser.data})
        else:
            cars = CarTable.objects.filter(id=id).first()
            ser = CarSer(cars)
            return Response({'code': 200, 'list': ser.data})

    def delete(self, request):
        id = request.GET.get('id')
        CarTable.objects.filter(id=id).delete()
        return Response({'code': 200})

    def put(self, request):
        # 获取ID
        id = request.data.get('id')
        # 通过ID获取该条数据
        cars = CarTable.objects.filter(id=id).first()
        # 进行修改
        cars.name = request.data.get('name')
        cars.money = request.data.get('money')
        cars.color = request.data.get('color')
        cars.types = request.data.get('types')
        cars.stutus = request.data.get('stutus')
        cars.content = request.data.get('content')
        # 最后保存
        cars.save()
        return Response({'code': 200})


class Delete(APIView):
    def post(self, request):
        id = request.data.get('id')
        CarTable.objects.filter(id__in=id).delete()
        return Response({'code': 200})

    def put(self, request):
        # 获取id
        id = request.GET.get('id')
        # 通过id查数据
        cars = CarTable.objects.filter(id=id).first()
        # 判断
        if cars.stutus:
            cars.stutus = False
            cars.save()
        else:
            cars.stutus = True
            cars.save()
        return Response({'code': 200})


# Create your views here.
from django.contrib.auth.hashers import check_password, make_password
from django.core.paginator import Paginator


class UserM(APIView):
    def post(self, request):
        request.data['password'] = make_password(request.data.get('password'))
        ser = UserSer(data=request.data)
        if ser.is_valid():
            ser.save()
            return Response({'code': 200})
        else:
            print(ser.errors)
            return Response({'code': 20010})


class ShopM(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user = Suser.objects.filter(username=username).first()
        if user:
            if check_password(password, user.password):
                return Response({'code': 200, 'userid': user.id})
            else:
                return Response({'code': 20011, 'mes': '密码错误'})
        else:
            return Response({'code': 20012, 'mes': '用户名错误'})

    def get(self, request):
        cpage = request.GET.get('cpage')
        shop = Shop.objects.all()
        p = Paginator(shop, 2)
        tpage = p.num_pages
        shops = p.page(cpage)
        ser = ShopSer(shops, many=True)
        return Response({'code': 200, 'list': ser.data, 'tpage': tpage})


class ComM(APIView):
    def post(self, request):
        ser = ComSer(data=request.data)
        id = request.data.get('shop')
        try:
            if ser.is_valid():
                ser.save()
                shop = Shop.objects.filter(id=id).first()
                shop.comments += 1
                shop.save()
                return Response({'code': 200})
        except:
            return Response({'code': 20015})
